const router = require('koa-router')();

router.prefix('/api')

// 写你自己的路由
//注册 
router.post('/registry', async ctx => {
    const {phone, passpwd} = ctx.request.body;
    console.log(phone,passpwd);
    if(phone && passpwd){
        const res=await ctx.searchQuery(`select * from logdata WHERE phone='${phone}'`);
        if(res.length){
            ctx.body={
                code:"FAIL",
                msg:"该用户已注册",
                status:200,
            }
            return;
     }
      //否则向仓库插入数据
      const addres= await ctx.searchQuery(`insert into logdata (phone,passpwd) values ('${phone}' , '${passpwd}')`);
      console.log(addres);
      ctx.body={
        code:"FAIL",
        msg:"注册成功", 
        status:200,
    }
     
    }
    else{

        ctx.body={
            code:"FAIL",
            msg:"请输入你的手机号或密码", 
            status:200,
        }
    }

})
router.post("/login",async (ctx)=>{
    const  { phone, passpwd } = ctx.request.body;
    console.log(phone , passpwd);
    const res=await ctx.searchQuery(`select * from logdata WHERE phone='${phone}' AND passpwd='${passpwd}'`);
    console.log(res);
   if(res.length){
            ctx.body={
                code:"200",
                msg:"登录成功",
                status:200,
            }
   }
   else{
    ctx.body={
        code:"404",
        msg:"登录失败",
        status:404,
    }
   }
})
//查询成绩从过id查询 或者全部查询
router.get("/seach",async(ctx)=>{
    const {id,pageNum,pageSize,oder=false,cloumeName="result"}=ctx.query; 
    if(id){
        const res=await ctx.searchQuery(`select * from conser WHERE  id='${id}'`);
        console.log(res);
        if(res.length){
            ctx.body={
                code:200,
                msg:"查询到数据了",
            }
        }
        else{
            ctx.body={
                code:404,
                msg:"查询失败",
            }
        }
        return;
    }
    console.log(pageNum,pageSize);
    const listRes =await ctx.searchQuery(`select * from conser order by ${cloumeName} ${oder ?"DESC":"ASC"} limit 10 offset 0`);
    console.log(listRes);
    ctx.body={
        code:200,
        msg:"数据查询成功",
        data:listRes
    }
})
//添加成绩
router.post("/addData",async(ctx)=>{
    const {name,result}=ctx.request.body;
    console.log(name,result);
    const address= await ctx.searchQuery(`insert into conser (name,result) values ('${name}' , '${result})`);

    if(address.length){
        
        ctx.body={
            code:200,
            msg:"添加成功"
        }
        return;
    }
    
})
//删除
router.delete("/delData", async ctx=>{
    let {id="[]"}=ctx.query;
    console.log(id);
    id=JSON.parse(id);
    if(Array.isArray(id)){
        const res= await ctx.searchQuery(`delete from conser where id in (${id.map(val=>val*1).join()})`);
        console.log(res);
        ctx.body={
            code:"FAIL",
            msg:"删除成功",
        }
        return;
    }
    ctx.body={
        code:"FAIL",
        msg:"参数id必须是数组",
    }
})
//查询
router.get("/searchName",async ctx=>{
    const {val}=ctx.query;
    console.log(val);
    let res=await ctx.searchQuery(`select * from conser where name like '%${val}%'`);
    console.log(res);
    
    if(res.length){
        ctx.body={
            code:200,
            msg:"有参数"
        }
    }else{
        ctx.body={
            code:404,
            msg:"无参数"
        }
    }
    
})


module.exports = router;